//
//  GSLoginView.m
//  GameSky
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSLoginView.h"
@interface GSLoginView () <UITextFieldDelegate>
@end
@implementation GSLoginView
#pragma mark------------------初始化方法-----------------------
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 夜间模式
        self.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        CGFloat height = frame.size.height;
        CGFloat minRow = height / 2 / 12;
        CGFloat minHeight = height / 2 / 6;
        
        // 用户名输入框
        self.userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(minRow, minHeight, kScreenWidth - 2 * minRow, minHeight)];
        
        
        // 左视图
        UIImageView *imageVUser = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, minHeight, minHeight)];
        
        _userNameTextField.leftView = imageVUser;
        imageVUser.image = [UIImage imageNamed:@"common_Icon_Account_16x16_UIMode_Day@2x"];
//        imageVUser.transform = CGAffineTransformScale(imageVUser.transform, 0.5, 0.5);
        
        imageVUser.contentMode =  UIViewContentModeCenter;
        
        
        _userNameTextField.leftViewMode = UITextFieldViewModeAlways;
        //代理
        _userNameTextField.delegate = self;
        _userNameTextField.borderStyle = UITextBorderStyleRoundedRect;

// 从 本地获取  用户名 字符串
        NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
        if (userName.length == 0 || userName == nil) {
            _userNameTextField.placeholder = @"请输入用户名";
        }else{
            _userNameTextField.text = userName;
        }
        // clear按钮
        _userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self addSubview:_userNameTextField];
// 密码输入框
        self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(_userNameTextField.left, _userNameTextField.bottom + 14, _userNameTextField.width, _userNameTextField.height)];
        _passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
        // 左视图
        UIImageView *imageVPassword = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, minHeight, minHeight)];
        imageVPassword.image = [UIImage imageNamed:@"common_Icon_PasswordLock_16x16_UIMode_Day@2x"];
        imageVPassword.contentMode =  UIViewContentModeCenter;
        
        // 代理
        _passwordTextField.delegate = self;
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
        _passwordTextField.leftView = imageVPassword;
        _passwordTextField.placeholder = @"请输入密码";
        _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self addSubview:_passwordTextField];
// 登陆按钮
        self.loginButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _loginButton.frame = CGRectMake(_passwordTextField.left, _passwordTextField.bottom + 14, _passwordTextField.width, _passwordTextField.height);
        [_loginButton setTitle:@"登录" forState:(UIControlStateNormal)];
        _loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        _loginButton.backgroundColor = [UIColor colorWithRed:155/255.0 green:37/255.0 blue:34/255.0 alpha:1.0];
        [self addSubview:_loginButton];
// 忘记密码
//        self.forgotPasswordButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        _forgotPasswordButton.frame = CGRectMake(_loginButton.left, _loginButton.bottom + 14, _loginButton.width / 4, minRow);
//        _forgotPasswordButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//        [_forgotPasswordButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
//        [_forgotPasswordButton setTitle:@"忘记密码" forState:(UIControlStateNormal)];
//        [self addSubview:_forgotPasswordButton];
// 注册
        self.registerButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _registerButton.frame = CGRectMake(_loginButton.right - _loginButton.width / 3, _loginButton.bottom + 14, _loginButton.width / 3, minRow);
        [_registerButton setTitle:@"注册新账号" forState:(UIControlStateNormal)];
        [_registerButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        _registerButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [self addSubview:_registerButton];
//  横线
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(_loginButton.left, height / 2, _loginButton.width, 1)];
        lineView.backgroundColor = [UIColor grayColor];
        [self addSubview:lineView];
// 添加 提示第三方 label 登陆的label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(lineView.width / 2 - _loginButton.width * 0.34 / 2, -minRow / 2, _loginButton.width * 0.34, minRow)];
        label.text = @"其他登录方式";
        label.font = [UIFont systemFontOfSize:14];
        label.layer.borderColor = [UIColor grayColor].CGColor;
        label.layer.borderWidth = 1;
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor whiteColor];
        [lineView addSubview:label];
// 添加 第三方登陆 按钮(QQ)
        self.QQLoginButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        CGFloat QQWidth = kScreenWidth / 16 * 3;
        _QQLoginButton.frame = CGRectMake((kScreenWidth - QQWidth) / 2, lineView.bottom + 36, QQWidth, QQWidth + 20);
        // 添加图片imageV
        UIImageView *buttonImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _QQLoginButton.width, _QQLoginButton.width)];;

        buttonImageV.image = [UIImage imageNamed:@"common_Icon_QQ_60x60_UIMode_Day@2x"];
        buttonImageV.layer.cornerRadius = _QQLoginButton.width / 2;
        buttonImageV.layer.masksToBounds = YES;
        [_QQLoginButton addSubview:buttonImageV];
        // 添加 label
        UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, buttonImageV.bottom, _QQLoginButton.width, _QQLoginButton.height - buttonImageV.bottom)];
        buttonLabel.textAlignment = NSTextAlignmentCenter;
        buttonLabel.text = @"QQ账号";
        buttonLabel.textColor = [UIColor grayColor];
        buttonLabel.font = [UIFont systemFontOfSize:14];
        [_QQLoginButton addSubview:buttonLabel];
        [self addSubview:_QQLoginButton];
    }
    return self;
}
#pragma mark--------------------回收键盘----------------------
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}
#pragma mark--------------------------------------------------
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
